package model

import (
	"gorm.io/gorm"
	"time"
	"user_service/global"
)

type Vote struct {
	Id               uint           `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	RemainingTickets uint           `gorm:"column:remaining_tickets;type:bigint;not null;" json:"remaining_tickets"`
	VoteType         uint           `gorm:"column:vote_type;type:int;comment:1.机票 2.车票 3.火车票;not null;" json:"vote_type"`     // 1.机票 2.车票 3.火车票
	VoteStatus       uint           `gorm:"column:vote_status;type:int;comment:1.有坐 2.候补 3.无座;not null;" json:"vote_status"`  // 1.有坐 2.候补 3.无座
	TrainNumber      string         `gorm:"column:train_number;type:varchar(20);comment:车次;not null;" json:"train_number"`    // 车次
	Seat             string         `gorm:"column:seat;type:varchar(20);comment:座位信息;not null;" json:"seat"`                  // 座位信息
	Price            float64        `gorm:"column:price;type:decimal(10, 2);comment:价格;not null;" json:"price"`               // 价格
	SeatType         uint           `gorm:"column:seat_type;type:int;comment:1.硬座 2.软座 3.硬卧 4.软卧;not null;" json:"seat_type"` // 1.硬座 2.软座 3.硬卧 4.软卧
	CreatedAt        time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt        time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt        gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (v *Vote) TableName() string {
	return "vote"
}

func (v *Vote) Created() error {
	return global.DB.Create(&v).Error
}

func (v *Vote) List(VoteStatus string) (list []*Vote, err error) {
	err = global.DB.Where("vote_status=?", VoteStatus).Find(&list).Error
	if err != nil {
		return
	}
	return nil, err
}

func (v *Vote) Info(id uint) error {
	return global.DB.Where("id=?", id).Limit(1).First(&v).Error
}

func (v *Vote) Info2(VoteType string) error {
	return global.DB.Where("VoteType=?", VoteType).Limit(1).First(&v).Error
}
